# 用于测试发送私有协议通讯
import json
import socket
import threading
import time
import uuid

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 9000))

def doPing(socket):
    imei = str(uuid.uuid1())
    ## 基础数据  command 9999 十六进制 0x270f
    command = 9999
    version = 1
    clientType = 4
    appId = 10000
    parseType = 0x0
    userId = 'ls1'

    ## 数据转换为byte类型
    commandBtye = command.to_bytes(4, 'big')
    versionByte = version.to_bytes(4, 'big')
    clientTypeByte = clientType.to_bytes(4, 'big')
    parseTypeByte = parseType.to_bytes(4, 'big')
    appIdByte = appId.to_bytes(4, 'big')
    imeiBytes = bytes(imei, "utf-8")
    imeiLength = len(imeiBytes)
    imeiLengthByte = imeiLength.to_bytes(4, 'big')
    data = {"userId": userId, "appId": appId, "clientType": clientType, "imei": imei}
    jsonData = json.dumps(data)
    body = bytes(jsonData, 'utf-8')
    body_len = len(body)
    bodyLenBytes = body_len.to_bytes(4, 'big')

    socket.sendall(
        commandBtye + versionByte + clientTypeByte + parseTypeByte + appIdByte + imeiLengthByte + bodyLenBytes + imeiBytes + body)

def ping(socket):
    while True:
        # 线程睡眠10s
        time.sleep(10)
        doPing(socket)


t1 = threading.Thread(target=ping,args=(s,))
t1.start()
